home *** CD-ROM | disk | FTP | other *** search
/ Delphi Magazine Collection 2001 / Delphi Magazine Collection 20001 (2001).iso / DISKS / Issue36 / Clinic / ShutDownU.pas < prev   
Encoding:
Pascal/Delphi Source File  |  1998-04-14  |  1.7 KB  |  69 lines

  1. unit ShutDownU;
  2.  
  3. interface
  4.  
  5. uses
  6.   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  7.   StdCtrls;
  8.  
  9. type
  10.   TForm1 = class(TForm)
  11.     btnRestart: TButton;
  12.     procedure btnRestartClick(Sender: TObject);
  13.   private
  14.     { Private declarations }
  15.   public
  16.     { Public declarations }
  17.   end;
  18.  
  19. var
  20.   Form1: TForm1;
  21.  
  22. implementation
  23.  
  24. {$R *.DFM}
  25.  
  26. {$ifdef Ver90} { Delphi 2.0x }
  27.   {$define Delphi32LessThan3}
  28. {$endif}
  29. {$ifdef Ver93} { C++ Builder 1.0x }
  30.   {$define Delphi32LessThan3}
  31. {$endif}
  32.  
  33. {$ifdef Delphi32LessThan3}
  34. procedure Win32Check(Value: Boolean);
  35. begin
  36.   if not Value then
  37.     raise Exception.Create(SysErrorMessage(GetLastError))
  38. end;
  39. {$endif}
  40.  
  41. procedure TForm1.btnRestartClick(Sender: TObject);
  42. var
  43.   HToken: THandle;
  44.   TP, OldTP: TTokenPrivileges;
  45.   ReturnLen: Integer;
  46. begin
  47.   if Win32Platform = VER_PLATFORM_WIN32_NT then
  48.   begin
  49.     //Get a token for this process
  50.     Win32Check(OpenProcessToken(GetCurrentProcess(),
  51.       TOKEN_ADJUST_PRIVILEGES or TOKEN_QUERY,
  52.       {$ifdef Delphi32LessThan3}@{$endif}HToken));
  53.     //Get the LUID for the shutdown privilege
  54.     Win32Check(LookupPrivilegeValue(nil,
  55.       'SeShutdownPrivilege', TP.Privileges[0].Luid));
  56.     TP.PrivilegeCount := 1; //One privilege to set
  57.     TP.Privileges[0].Attributes := SE_PRIVILEGE_ENABLED;
  58.     //Acquire shutdown privilege for this process
  59.     Win32Check(AdjustTokenPrivileges(HToken,
  60.       False, TP, SizeOf(OldTP), OldTP, ReturnLen));
  61.   end;
  62.   //Shut down the system and force all applications to close
  63.   if not ExitWindowsEx(EWX_SHUTDOWN {or EWX_FORCE}, 0) then
  64.     raise Exception.Create('Cannot shut Windows');
  65.   Close;
  66. end;
  67.  
  68. end.
  69.